package com.jettech.oa.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import com.jettech.oa.common.ConstType;
import com.jettech.oa.domain.Resource;
import com.jettech.oa.domain.User;
import com.jettech.oa.dto.Menu;
import com.jettech.oa.service.UserService;

@Controller
@SessionAttributes("ADMIN_SESSION_USER")
@RequestMapping(value="/system")
public class AdminLoginController {
	@Autowired
	private UserService userService;
	/**
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String toLogin() {
		return "system/login";
	}
	/**
	 * @param loginName
	 * @param loginPassword
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public String login(@RequestParam String loginName,@RequestParam String loginPassword,ModelMap model,HttpServletRequest request) {
		User user = userService.getUser(loginName);
		if(user!=null && user.getLoginPassword().equals(loginPassword)){
			model.put(ConstType.ADMIN_SESSION_USER, user);
			if(user.getRole()!=null && user.getRole().getId()!=null){
				List<Resource> userOperationList = userService.findUserOperationResourceList(user.getRole().getId());
				if(userOperationList!=null && userOperationList.size()>0){
					Map<String,String> userOperationURIMap = new HashMap<String,String>();
					List<Menu> menus = new ArrayList<Menu>();
					Map<Long,List<Menu>> map = new HashMap<Long,List<Menu>>();
					for(Resource resource:userOperationList){
						if("1".equals(resource.getIsMenu())){
							Menu menu = new Menu();
							menu.setId(resource.getId());
							menu.setName(resource.getName());
							menu.setUrl(resource.getUrl());
							if(resource.getParentResource()!=null){
								userOperationURIMap.put(resource.getUrl(), resource.getUrl());
								if(map.containsKey(resource.getParentResource().getId())){
									map.get(resource.getParentResource().getId()).add(menu);
								}else{
									List<Menu> list = new ArrayList<Menu>();
									list.add(menu);
									map.put(resource.getId(), list);
								}
							}else{
								List<Menu> list = new ArrayList<Menu>();
								map.put(resource.getId(), list);
								menus.add(menu);
							}
						}else{
							if(resource.getParentResource()!=null){
								userOperationURIMap.put(resource.getUrl(), resource.getUrl());
							}	
						}
					}
					for(Menu menu:menus){
						if(map.containsKey(menu.getId())){
							menu.setMenus(map.get(menu.getId()));
						}
					}
					request.getSession().setAttribute(ConstType.USER_MENUS, menus);
					request.getSession().setAttribute(ConstType.USER_OPERATION_URIS_MAP, userOperationURIMap);
				}
			}
			return "redirect:/system/main";
		}else{
			return "redirect:/system/login";
		}
	}
	/**
	 * @param loginName
	 * @param loginPassword
	 * @return
	 */
	@RequestMapping(value="/main")
	public String index(HttpServletRequest request) {
		if(request.getSession().getAttribute(ConstType.ADMIN_SESSION_USER)!=null){
			return "system/index";
		}
		return "system/login";
	}
	/**
	 * @return
	 */
	@RequestMapping(value="/top")
	public String top() {
		return "system/top";
	}
	/**
	 * @return
	 */
	@RequestMapping(value="/menu")
	public String menu() {
		return "system/leftMenu";
	}
	/**
	 * @return
	 */
	@RequestMapping(value="/drag")
	public String drag() {
		return "system/drag";
	}
	/**
	 * @param status
	 * @return
	 */
	@RequestMapping(value="/logout",method=RequestMethod.GET)
	public String logout(SessionStatus status) {
		status.setComplete();
		return "system/login";
	}
}
